package controller;

import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import dao.CompanyDao;
import dto.CompanyDto;

@Controller
public class CompanyController {

	@Autowired
	private CompanyDao companyService;

	@RequestMapping(value = "/companyList.html", method = RequestMethod.GET)
    public ModelAndView companyList(HttpSession session, HttpServletRequest request) throws NoSuchFieldException, SecurityException, IOException, URISyntaxException {
        System.out.println("회사 리스트 출력");
		ModelAndView mv = new ModelAndView();
		String type = request.getParameter("type");
		if(type == null) {
			type = "1"; //전체
		}
		
        ArrayList<?> companyList = this.companyService.companyList(type);	//	회원 아이디를 넘겨주므로서 아이디와 일치하는 자료들 리스트를 받을 수 있음;
        System.out.println("회사 리스트 : " + companyList);
        mv.setViewName("index");
        mv.addObject("content", "/WEB-INF/company/companyListForm.jsp"); // 바뀔 페이지값 넘겨줌
        mv.addObject("companyList", companyList);	//해당 페이지에서 el로 값 받으면됨
        return mv ;
    }

	@RequestMapping(value = "/companyReg.html", method = RequestMethod.POST)
    public ModelAndView companySubmit(HttpServletRequest request, CompanyDto dto, HttpSession session) throws ServletRequestBindingException{
    	System.out.println("회사 등록");
    	String s_year =  request.getParameter("s_year");
    	String s_month = request.getParameter("s_month");
    	String s_day = request.getParameter("s_day");
    	String e_year = request.getParameter("e_year");
    	String e_month = request.getParameter("e_month");
    	String e_day = request.getParameter("e_day");
    	
    	String s_date = s_year+"-"+s_month+"-"+s_day;
    	String e_date = e_year+"-"+e_month+"-"+e_day;
    	
    	dto.setS_date(s_date);
    	dto.setE_date(e_date);
    	
        int row = this.companyService.insertCompany(dto);
        if(row > 0) {
        	String url = "redirect:/companyList.html";
        	return new ModelAndView(url);
        }
        else {
        	String url = "redirect:/error1.html";
        	return new ModelAndView(url);
        }
    }
    
    @RequestMapping(value = "/error1.html", method = RequestMethod.GET)
    public ModelAndView error() {
		System.out.println("에러 처리 메소드");
    	ModelAndView mv = new ModelAndView();
    	String type = "등록 실패";
    	mv.setViewName("/error/error");
    	mv.addObject("type", type);
        return mv;
    }
    
    @RequestMapping(value = "/error2.html", method = RequestMethod.GET)
    public ModelAndView error2() {
		System.out.println("에러 처리 메소드2");
    	ModelAndView mv = new ModelAndView();
    	String type = "삭제 실패";
    	mv.setViewName("/error/error");
    	mv.addObject("type", type);
        return mv;
    }
    
    @RequestMapping(value = "/companyRegView.html", method = RequestMethod.GET)
    public ModelAndView companyRegView() {
		System.out.println("회사 등록페이지로 이동");
    	ModelAndView mv = new ModelAndView();
    	mv.setViewName("index");
    	mv.addObject("content", "/WEB-INF/company/companyRegForm.jsp");
        return mv;
    }
    
    
	@RequestMapping(value = "/companyDelete.html", method = RequestMethod.GET)
    public ModelAndView companyDelete(HttpSession session, HttpServletRequest request, CompanyDto dto) {
		System.out.println("회사 삭제");
		
        int row = this.companyService.removeCompany(dto); //파일 DB 삭제
        if(row > 0) {
        	String url = "redirect:/companyList.html";
        	return new ModelAndView(url);
        }
        else {
        	String url = "redirect:/error2.html";
        	return new ModelAndView(url);
        }
    } 
}
